package com.kxmall.web.controller.huishouapp;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.coupon.domain.vo.KxStoreCouponUserVo;
import com.kxmall.huishou.domain.HsRecyclerApply;
import com.kxmall.huishou.domain.bo.HsRecyclerApplyBo;
import com.kxmall.huishou.domain.vo.HsRecyclerApplyVo;
import com.kxmall.huishou.service.IHsRecyclerApplyService;
import com.kxmall.huishou.service.IHsWasteSiteService;
import com.kxmall.web.controller.coupon.service.IKxAppCouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 回收人注册
 *
 * @author lf
 * @date 2023-12-01
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/recycler/app")
public class HsAppRecyclerRegistrationController extends BaseAppController {

    private final IHsRecyclerApplyService iHsRecyclerApplyService;



    /**
     * 获取回收员申请详细信息
     *
     */
    @GetMapping("/getRecyclerApplyInfo")
    public R<HsRecyclerApplyVo> getRecyclerApplyInfo() {

        QueryWrapper<HsRecyclerApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",getLoginUser().getUserId());
        queryWrapper.orderByDesc("create_time");

        return R.ok(iHsRecyclerApplyService.queryByWrapper(queryWrapper));
    }

    /**
     * 新增回收员申请信息
     */
    @Log(title = "新增回收员申请信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HsRecyclerApplyBo bo) {
        Long userId = getLoginUser().getUserId();
        QueryWrapper<HsRecyclerApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
//        List<Long> statusList = new ArrayList<>();
//        statusList.add(0l);
//        statusList.add(1l);
//        queryWrapper.in("status", statusList);
        HsRecyclerApplyVo hsRecyclerApplyVo = iHsRecyclerApplyService.queryByWrapper(queryWrapper);
        if(hsRecyclerApplyVo != null){
            return R.fail("您已提交过申请，不能重复提交！");
        }
        bo.setUserId(userId);
        bo.setStatus(0l);

        return toAjax(iHsRecyclerApplyService.insertByBo(bo));
    }

    /**
     * 失败后修改申请再次提交信息
     */
    @SaIgnore
    @Log(title = "修改回收员申请信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody HsRecyclerApplyBo bo) {

        Long userId = getLoginUser().getUserId();
        QueryWrapper<HsRecyclerApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("id",bo.getId());
        HsRecyclerApplyVo hsRecyclerApplyVo = iHsRecyclerApplyService.queryByWrapper(queryWrapper);
        if(hsRecyclerApplyVo == null || hsRecyclerApplyVo.getStatus() == null || hsRecyclerApplyVo.getStatus() != 2){
            return R.fail("申请信息不存在或者该状态不能进行修改");
        }

        bo.setStatus(0l);

        Boolean boolStatus = iHsRecyclerApplyService.updateByBo(bo);

        return toAjax(boolStatus);
    }


}
